<?php

/**
 * Manage flirt table. This table store flirtings of users
 *
 * @author tuancasi
 * @property int $flirt_id
 * @property int $sender 
 * @property int $receiver
 * @property int $sender_read
 * @property int $receiver_read
 * @property date $time_created
 * 
 */
class Flirt extends CActiveRecord {

    const READ = 1;
    const NOT_READ = 0;

    public $userName;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{flirt}}';
    }

    public function rules() {
	return array(
	    array('flirt_id, sender, receiver, sender_read, receiver_read, time_created, userName', 'safe')
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'r_sender' => array(self::BELONGS_TO, 'User', 'sender'),
	    'r_receiver' => array(self::BELONGS_TO, 'User', 'receiver')
	);
    }

    /**
     * insert a flirt
     * 
     * @param type $sender id of user send
     * @param type $receiver id of user receive
     */
    public static function insertFlirt($sender, $receiver) {
	db()->createCommand('Insert into {{flirt}}(sender, receiver, time_created) VALUES(' . $sender . ', ' . $receiver . ', "' . date('Y-m-d', time()) . '")')->execute();
	$aFlirtReceivers = Yii::app()->cache->get('aFlirtReceivers' . $sender);
	if ($aFlirtReceivers !== FALSE) {
	    Yii::app()->cache->delete('aFlirtReceivers' . $sender);
	}
    }

    /**
     * get list flirts they sent
     */
    public static function getList($sender) {

	$aFlirtReceivers = Yii::app()->cache->get('aFlirtReceivers' . $sender);
	if ($aFlirtReceivers === FALSE) {
	    $aFlirtReceivers = array();
	    $aReceivers = db()->createCommand("SELECT receiver FROM {{flirt}} WHERE sender=" . $sender)->queryAll();
	    foreach ($aReceivers as $item) {
		$aFlirtReceivers[] = $item['receiver'];
	    }
	    Yii::app()->cache->set('aFlirtReceivers' . $sender, $aFlirtReceivers);
	}
	return $aFlirtReceivers;
    }

//    public static function deleteFlirt($id, $sender) {
//	db()->createCommand('DELETE FROM {{flirt}} WHERE flirt_id=' . $id)->execute();
//	$aFlirtReceivers = Yii::app()->cache->get('aFlirtReceivers');
//	if ($aFlirtReceivers) {
//	    Yii::app()->cache->delete('aFlirtReceivers' . $sender);
//	}
//    }

    public function afterDelete() {
	parent::afterDelete();
	$aFlirtReceivers = Yii::app()->cache->get('aFlirtReceivers' . $this->sender);
	if ($aFlirtReceivers !== false) {
	    Yii::app()->cache->delete('aFlirtReceivers' . $this->sender);
	}
    }

    /**
     * for displaying user flirted with grid view type
     */
    public function search($type) {
	$criteria = new CDbCriteria();
	if ($type == 'my_flirts') {
	    $criteria->with = 'r_receiver';
	    $criteria->addCondition('t.sender=' . user()->id);
	    $criteria->compare('r_receiver.username', $this->userName, true);
	} else {
	    $criteria->with = 'r_sender';
	    $criteria->addCondition('t.receiver=' . user()->id);
	    $criteria->compare('r_sender.username', $this->userName, true);
	}
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    'time_created' => CSort::SORT_DESC
		)
	    )
	));
    }

    public function getAvatar($type) {

	if ($type == 'my_flirts') {
	    return '<img src="' . $this->r_receiver->getAvatarUrl('small') . '"/>' . $type;
	} else {
	    return '<img src="' . $this->r_sender->getAvatarUrl('small') . '"/>';
	}
    }

}

